﻿using LeanApp.Bll;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;

namespace LeanApp.View
{
    /// <summary>
    /// QualityDataCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class QualityDataCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="bll">质量分析的业务</param>
        public QualityDataCtrl(string title, QualityBll bll)
        {
            InitializeComponent();
            _title = title;
            _bll = bll;
            InitialDataGrid();
            InitialData();
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        private void InitialDataGrid()
        {
            if (_bll != null)
            {
                Dictionary<string, string> colMapper = _bll.GetColMapper();
                if (colMapper != null && colMapper.Count > 0)
                {
                    foreach (string key in colMapper.Keys)
                    {
                        DataGridTextColumn col = new DataGridTextColumn { Header = colMapper[key], Binding = new Binding(key), Width = DataGridLength.SizeToHeader };
                        grid.Columns.Add(col);
                    }
                }
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitialData()
        {
            title.Text = _title;
            if (_bll != null && _bll.AllItem != null && _bll.AllItem.Count > 0)
            {
                _bll.AllItem.ForEach(a =>
                {
                    grid.Items.Add(a);
                });
            }
        }

        /// <summary>
        /// 质量分析的业务
        /// </summary>
        private QualityBll _bll = null;

        /// <summary>
        /// 标题
        /// </summary>
        private string _title = "";
    }
}
